{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 对print出的数据格式进行修正\n",
    "pd.set_option('expand_frame_repr', False)  # 当列太多时不换行\n",
    "pd.set_option('precision', 2)  # 浮点数的精度\n",
    "pd.set_option('display.max_rows', 10) # 显示的最大行数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>candle_begin_time</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>北京时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-01-24 00:00:00</td>\n",
       "      <td>10812.0</td>\n",
       "      <td>10895.00</td>\n",
       "      <td>10812.0</td>\n",
       "      <td>10886.00</td>\n",
       "      <td>77.54</td>\n",
       "      <td>08点00分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-01-24 00:01:00</td>\n",
       "      <td>10886.0</td>\n",
       "      <td>10906.00</td>\n",
       "      <td>10871.0</td>\n",
       "      <td>10871.00</td>\n",
       "      <td>60.05</td>\n",
       "      <td>08点01分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-01-24 00:02:00</td>\n",
       "      <td>10872.0</td>\n",
       "      <td>10872.00</td>\n",
       "      <td>10840.0</td>\n",
       "      <td>10840.00</td>\n",
       "      <td>30.69</td>\n",
       "      <td>08点02分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-24 00:03:00</td>\n",
       "      <td>10840.0</td>\n",
       "      <td>10849.00</td>\n",
       "      <td>10815.0</td>\n",
       "      <td>10833.00</td>\n",
       "      <td>20.13</td>\n",
       "      <td>08点03分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-01-24 00:04:00</td>\n",
       "      <td>10834.0</td>\n",
       "      <td>10853.00</td>\n",
       "      <td>10833.0</td>\n",
       "      <td>10847.00</td>\n",
       "      <td>25.56</td>\n",
       "      <td>08点04分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1435</th>\n",
       "      <td>2018-01-24 23:55:00</td>\n",
       "      <td>11284.0</td>\n",
       "      <td>11326.00</td>\n",
       "      <td>11284.0</td>\n",
       "      <td>11308.00</td>\n",
       "      <td>64.43</td>\n",
       "      <td>07点55分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1436</th>\n",
       "      <td>2018-01-24 23:56:00</td>\n",
       "      <td>11310.0</td>\n",
       "      <td>11347.00</td>\n",
       "      <td>11310.0</td>\n",
       "      <td>11346.11</td>\n",
       "      <td>56.94</td>\n",
       "      <td>07点56分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1437</th>\n",
       "      <td>2018-01-24 23:57:00</td>\n",
       "      <td>11346.0</td>\n",
       "      <td>11380.00</td>\n",
       "      <td>11338.0</td>\n",
       "      <td>11361.00</td>\n",
       "      <td>100.94</td>\n",
       "      <td>07点57分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1438</th>\n",
       "      <td>2018-01-24 23:58:00</td>\n",
       "      <td>11361.0</td>\n",
       "      <td>11427.00</td>\n",
       "      <td>11361.0</td>\n",
       "      <td>11407.00</td>\n",
       "      <td>86.06</td>\n",
       "      <td>07点58分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1439</th>\n",
       "      <td>2018-01-24 23:59:00</td>\n",
       "      <td>11410.0</td>\n",
       "      <td>11427.88</td>\n",
       "      <td>11391.0</td>\n",
       "      <td>11391.00</td>\n",
       "      <td>77.19</td>\n",
       "      <td>07点59分00秒</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1440 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        candle_begin_time     open      high      low     close  volume       北京时间\n",
       "0     2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54  08点00分00秒\n",
       "1     2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05  08点01分00秒\n",
       "2     2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69  08点02分00秒\n",
       "3     2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13  08点03分00秒\n",
       "4     2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56  08点04分00秒\n",
       "...                   ...      ...       ...      ...       ...     ...        ...\n",
       "1435  2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43  07点55分00秒\n",
       "1436  2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94  07点56分00秒\n",
       "1437  2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94  07点57分00秒\n",
       "1438  2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06  07点58分00秒\n",
       "1439  2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19  07点59分00秒\n",
       "\n",
       "[1440 rows x 7 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入数据\n",
    "df = pd.read_csv(\n",
    "    # 该参数为数据在电脑中的路径，标题最好别带中文\n",
    "    filepath_or_buffer = r'C:\\notebooks\\quantitative_trading_notes\\data\\BITFINEX_BTCUSD_20180124_1T.csv',\n",
    "    # 该参数代表数据的分隔符，csv文件默认是逗号。其他常见的是'\\t'\n",
    "    sep=',',\n",
    "    # 该参数代表跳过数据文件的的第1行不读入\n",
    "    skiprows=1,\n",
    "    # nrows，只读取前n行数据，若不指定，读入全部的数据\n",
    "    # nrows=15,\n",
    "    # 将指定列的数据识别为日期格式。若不指定，时间数据将会以字符串形式读入。一开始先不用。\n",
    "    # parse_dates=['candle_begin_time'],\n",
    "    # 将指定列设置为index。若不指定，index默认为0, 1, 2, 3, 4...\n",
    "    # index_col=['candle_begin_time'],\n",
    "    # 读取指定的这几列数据，其他数据不读取。若不指定，读入全部列\n",
    "    # usecols=['candle_begin_time', 'close'],\n",
    "    # 当某行数据有问题时，报错。设定为False时即不报错，直接跳过该行。当数据比较脏乱的时候用这个。\n",
    "    # error_bad_lines=False,\n",
    "    # 将数据中的null识别为空值\n",
    "    # na_values='NULL',\n",
    "\n",
    "    # 更多其他参数，请直接搜索\"pandas read_csv\"，要去逐个查看一下。比较重要的，header等\n",
    "    # 还有read_table、read_excel、read_json等，他们的参数内容都是大同小异，可以自行搜索查看。\n",
    "\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape: (1440, 7)\n",
      "shape[0]: 1440\n",
      "columns 列名: Index(['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', '北京时间'], dtype='object')\n",
      "index 行名: RangeIndex(start=0, stop=1440, step=1)\n",
      "**********\n",
      "dtypes: candle_begin_time     object\n",
      "open                 float64\n",
      "high                 float64\n",
      "low                  float64\n",
      "close                float64\n",
      "volume               float64\n",
      "北京时间                  object\n",
      "dtype: object\n",
      "**********\n",
      "     candle_begin_time     open     high      low    close  volume       北京时间\n",
      "0  2018-01-24 00:00:00  10812.0  10895.0  10812.0  10886.0   77.54  08点00分00秒\n",
      "1  2018-01-24 00:01:00  10886.0  10906.0  10871.0  10871.0   60.05  08点01分00秒\n",
      "2  2018-01-24 00:02:00  10872.0  10872.0  10840.0  10840.0   30.69  08点02分00秒\n",
      "        candle_begin_time     open      high      low    close  volume       北京时间\n",
      "1437  2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.0  100.94  07点57分00秒\n",
      "1438  2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.0   86.06  07点58分00秒\n",
      "1439  2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.0   77.19  07点59分00秒\n",
      "       candle_begin_time     open      high      low    close  volume       北京时间\n",
      "629  2018-01-24 10:29:00  11018.0  11018.00  10981.0  10991.0   12.14  18点29分00秒\n",
      "109  2018-01-24 01:49:00  10681.0  10685.76  10675.0  10678.0    6.71  09点49分00秒\n",
      "149  2018-01-24 02:29:00  10690.0  10712.00  10689.0  10703.0   12.04  10点29分00秒\n",
      "           open      high       low     close   volume\n",
      "count   1440.00   1440.00   1440.00   1440.00  1440.00\n",
      "mean   10986.52  10999.73  10973.26  10986.84    30.46\n",
      "std      231.77    232.44    231.85    232.05    39.61\n",
      "min    10487.00  10513.00  10430.00  10482.00     0.37\n",
      "25%    10790.52  10801.00  10780.75  10793.72     8.66\n",
      "50%    11015.50  11029.00  11001.00  11016.00    18.96\n",
      "75%    11181.00  11193.00  11170.00  11181.00    38.31\n",
      "max    11515.00  11557.89  11499.00  11516.00   520.39\n"
     ]
    }
   ],
   "source": [
    "# 看数据\n",
    "print('shape:', df.shape)  # 输出dataframe有多少行、多少列。\n",
    "print('shape[0]:', df.shape[0])  # 取行数量，相应的列数量就是df.shape[1]\n",
    "print('columns 列名:', df.columns)  # 顺序输出每一列的名字，演示如何for语句遍历。\n",
    "print('index 行名:', df.index)  # 顺序输出每一行的名字，可以for语句遍历。\n",
    "\n",
    "print('*' * 10)\n",
    "print('dtypes:', df.dtypes)  # 数据每一列的类型不一样，比如数字、字符串、日期等。该方法输出每一列变量类型\n",
    "\n",
    "print('*' * 10)\n",
    "print(df.head(3))  # 看前3行的数据，默认是5。与自然语言很接近\n",
    "print(df.tail(3))  # 看最后3行的数据，默认是5。\n",
    "print(df.sample(n=3))  # 随机抽取3行，想要去固定比例的话，可以用frac参数\n",
    "print(df.describe())  # 非常方便的函数，对每一列数据有直观感受；只会对数字类型的列有效"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       candle_begin_time     open      high      low     close  volume       北京时间\n",
      "0    2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54  08点00分00秒\n",
      "1    2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05  08点01分00秒\n",
      "2    2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69  08点02分00秒\n",
      "3    2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13  08点03分00秒\n",
      "4    2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56  08点04分00秒\n",
      "...                  ...      ...       ...      ...       ...     ...        ...\n",
      "1435 2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43  07点55分00秒\n",
      "1436 2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94  07点56分00秒\n",
      "1437 2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94  07点57分00秒\n",
      "1438 2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06  07点58分00秒\n",
      "1439 2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19  07点59分00秒\n",
      "\n",
      "[1440 rows x 7 columns]\n",
      "dtypes: candle_begin_time    datetime64[ns]\n",
      "open                        float64\n",
      "high                        float64\n",
      "low                         float64\n",
      "close                       float64\n",
      "volume                      float64\n",
      "北京时间                         object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "df['candle_begin_time'] = pd.to_datetime(df['candle_begin_time'])\n",
    "print(df)\n",
    "print('dtypes:', df.dtypes)  # 数据每一列的类型不一样，比如数字、字符串、日期等。该方法输出每一列变量类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>北京时间</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>candle_begin_time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:00:00</th>\n",
       "      <td>10812.0</td>\n",
       "      <td>10895.00</td>\n",
       "      <td>10812.0</td>\n",
       "      <td>10886.00</td>\n",
       "      <td>77.54</td>\n",
       "      <td>08点00分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:01:00</th>\n",
       "      <td>10886.0</td>\n",
       "      <td>10906.00</td>\n",
       "      <td>10871.0</td>\n",
       "      <td>10871.00</td>\n",
       "      <td>60.05</td>\n",
       "      <td>08点01分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:02:00</th>\n",
       "      <td>10872.0</td>\n",
       "      <td>10872.00</td>\n",
       "      <td>10840.0</td>\n",
       "      <td>10840.00</td>\n",
       "      <td>30.69</td>\n",
       "      <td>08点02分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:03:00</th>\n",
       "      <td>10840.0</td>\n",
       "      <td>10849.00</td>\n",
       "      <td>10815.0</td>\n",
       "      <td>10833.00</td>\n",
       "      <td>20.13</td>\n",
       "      <td>08点03分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:04:00</th>\n",
       "      <td>10834.0</td>\n",
       "      <td>10853.00</td>\n",
       "      <td>10833.0</td>\n",
       "      <td>10847.00</td>\n",
       "      <td>25.56</td>\n",
       "      <td>08点04分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:55:00</th>\n",
       "      <td>11284.0</td>\n",
       "      <td>11326.00</td>\n",
       "      <td>11284.0</td>\n",
       "      <td>11308.00</td>\n",
       "      <td>64.43</td>\n",
       "      <td>07点55分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:56:00</th>\n",
       "      <td>11310.0</td>\n",
       "      <td>11347.00</td>\n",
       "      <td>11310.0</td>\n",
       "      <td>11346.11</td>\n",
       "      <td>56.94</td>\n",
       "      <td>07点56分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:57:00</th>\n",
       "      <td>11346.0</td>\n",
       "      <td>11380.00</td>\n",
       "      <td>11338.0</td>\n",
       "      <td>11361.00</td>\n",
       "      <td>100.94</td>\n",
       "      <td>07点57分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:58:00</th>\n",
       "      <td>11361.0</td>\n",
       "      <td>11427.00</td>\n",
       "      <td>11361.0</td>\n",
       "      <td>11407.00</td>\n",
       "      <td>86.06</td>\n",
       "      <td>07点58分00秒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:59:00</th>\n",
       "      <td>11410.0</td>\n",
       "      <td>11427.88</td>\n",
       "      <td>11391.0</td>\n",
       "      <td>11391.00</td>\n",
       "      <td>77.19</td>\n",
       "      <td>07点59分00秒</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1440 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                        open      high      low     close  volume       北京时间\n",
       "candle_begin_time                                                           \n",
       "2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54  08点00分00秒\n",
       "2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05  08点01分00秒\n",
       "2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69  08点02分00秒\n",
       "2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13  08点03分00秒\n",
       "2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56  08点04分00秒\n",
       "...                      ...       ...      ...       ...     ...        ...\n",
       "2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43  07点55分00秒\n",
       "2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94  07点56分00秒\n",
       "2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94  07点57分00秒\n",
       "2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06  07点58分00秒\n",
       "2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19  07点59分00秒\n",
       "\n",
       "[1440 rows x 6 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行名index、列名column 都叫做lable\n",
    "# position 也可以定位\n",
    "df.set_index([\"candle_begin_time\"], inplace=True)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "candle_begin_time\n",
      "2018-01-24 00:00:00    10886.00\n",
      "2018-01-24 00:01:00    10871.00\n",
      "2018-01-24 00:02:00    10840.00\n",
      "2018-01-24 00:03:00    10833.00\n",
      "2018-01-24 00:04:00    10847.00\n",
      "                         ...   \n",
      "2018-01-24 23:55:00    11308.00\n",
      "2018-01-24 23:56:00    11346.11\n",
      "2018-01-24 23:57:00    11361.00\n",
      "2018-01-24 23:58:00    11407.00\n",
      "2018-01-24 23:59:00    11391.00\n",
      "Name: close, Length: 1440, dtype: float64\n",
      "                        open     close\n",
      "candle_begin_time                     \n",
      "2018-01-24 00:00:00  10812.0  10886.00\n",
      "2018-01-24 00:01:00  10886.0  10871.00\n",
      "2018-01-24 00:02:00  10872.0  10840.00\n",
      "2018-01-24 00:03:00  10840.0  10833.00\n",
      "2018-01-24 00:04:00  10834.0  10847.00\n",
      "...                      ...       ...\n",
      "2018-01-24 23:55:00  11284.0  11308.00\n",
      "2018-01-24 23:56:00  11310.0  11346.11\n",
      "2018-01-24 23:57:00  11346.0  11361.00\n",
      "2018-01-24 23:58:00  11361.0  11407.00\n",
      "2018-01-24 23:59:00  11410.0  11391.00\n",
      "\n",
      "[1440 rows x 2 columns]\n"
     ]
    }
   ],
   "source": [
    "# 选取单列\n",
    "print(df['close']) # 展示出的是Series\n",
    "\n",
    "# 选取多列， 注意：需要两个[]   df[ list]   list = ['A', 'B']  \n",
    "# 因为我candle_begin_time已经设置了index，所以不能选择candle_begin_time\n",
    "print(df[['open','close']]) # 读取的数据是DataFrame类型 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "open        1.1e+04\n",
      "high        1.1e+04\n",
      "low         1.1e+04\n",
      "close       1.1e+04\n",
      "volume           60\n",
      "北京时间      08点01分00秒\n",
      "Name: 2018-01-24 00:01:00, dtype: object\n",
      "                        open     high      low    close  volume       北京时间\n",
      "candle_begin_time                                                         \n",
      "2018-01-24 00:01:00  10886.0  10906.0  10871.0  10871.0   60.05  08点01分00秒\n",
      "2018-01-24 00:02:00  10872.0  10872.0  10840.0  10840.0   30.69  08点02分00秒\n",
      "2018-01-24 00:03:00  10840.0  10849.0  10815.0  10833.0   20.13  08点03分00秒\n",
      "2018-01-24 00:04:00  10834.0  10853.0  10833.0  10847.0   25.56  08点04分00秒\n",
      "2018-01-24 00:05:00  10848.0  10849.0  10826.0  10826.0    7.84  08点05分00秒\n",
      "2018-01-24 00:06:00  10831.0  10839.0  10822.0  10834.0   11.76  08点06分00秒\n",
      "2018-01-24 00:07:00  10834.0  10835.0  10819.0  10820.0   14.96  08点07分00秒\n",
      "                        open      high      low     close\n",
      "candle_begin_time                                        \n",
      "2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00\n",
      "2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00\n",
      "2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00\n",
      "2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00\n",
      "2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00\n",
      "...                      ...       ...      ...       ...\n",
      "2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00\n",
      "2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11\n",
      "2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00\n",
      "2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00\n",
      "2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00\n",
      "\n",
      "[1440 rows x 4 columns]\n",
      "                        open     high      low    close\n",
      "candle_begin_time                                      \n",
      "2018-01-24 00:01:00  10886.0  10906.0  10871.0  10871.0\n",
      "2018-01-24 00:02:00  10872.0  10872.0  10840.0  10840.0\n",
      "2018-01-24 00:03:00  10840.0  10849.0  10815.0  10833.0\n",
      "2018-01-24 00:04:00  10834.0  10853.0  10833.0  10847.0\n",
      "2018-01-24 00:05:00  10848.0  10849.0  10826.0  10826.0\n",
      "                        open      high      low     close  volume       北京时间\n",
      "candle_begin_time                                                           \n",
      "2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54  08点00分00秒\n",
      "2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05  08点01分00秒\n",
      "2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69  08点02分00秒\n",
      "2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13  08点03分00秒\n",
      "2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56  08点04分00秒\n",
      "...                      ...       ...      ...       ...     ...        ...\n",
      "2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43  07点55分00秒\n",
      "2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94  07点56分00秒\n",
      "2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94  07点57分00秒\n",
      "2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06  07点58分00秒\n",
      "2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19  07点59分00秒\n",
      "\n",
      "[1440 rows x 6 columns]\n",
      "10812.0\n"
     ]
    }
   ],
   "source": [
    "# loc操作：通过label（columns和index的名字）来读取数据\n",
    "print(df.loc['2018-01-24 00:01:00'])  # 参数只能是行名index  选取指定的某一行，读取的数据是Series类型 \n",
    "print(df.loc['2018-01-24 00:01:00' : '2018-01-24 00:07:00'])  # 选取连续的几行\n",
    "print(df.loc[:, 'open':'close'])  # 选取在此范围内的多列，读取的数据是DataFrame类型\n",
    "print(df.loc['2018-01-24 00:01:00': '2018-01-24 00:05:00', 'open':'close'])  # 读取指定的多行、多列。逗号之前是行的范围，逗号之后是列的范围。读取的数据是DataFrame类型\n",
    "print(df.loc[:, :])  # 读取所有行、所有列，读取的数据是DataFrame类型\n",
    "print(df.loc['2018-01-24 00:00:00', 'open'])  # 据说at更高效，但是报错？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "open        1.1e+04\n",
      "high        1.1e+04\n",
      "low         1.1e+04\n",
      "close       1.1e+04\n",
      "volume           78\n",
      "北京时间      08点00分00秒\n",
      "Name: 2018-01-24 00:00:00, dtype: object\n",
      "                        open     high      low    close  volume       北京时间\n",
      "candle_begin_time                                                         \n",
      "2018-01-24 00:01:00  10886.0  10906.0  10871.0  10871.0   60.05  08点01分00秒\n",
      "2018-01-24 00:02:00  10872.0  10872.0  10840.0  10840.0   30.69  08点02分00秒\n",
      "                         high      low\n",
      "candle_begin_time                     \n",
      "2018-01-24 00:00:00  10895.00  10812.0\n",
      "2018-01-24 00:01:00  10906.00  10871.0\n",
      "2018-01-24 00:02:00  10872.00  10840.0\n",
      "2018-01-24 00:03:00  10849.00  10815.0\n",
      "2018-01-24 00:04:00  10853.00  10833.0\n",
      "...                       ...      ...\n",
      "2018-01-24 23:55:00  11326.00  11284.0\n",
      "2018-01-24 23:56:00  11347.00  11310.0\n",
      "2018-01-24 23:57:00  11380.00  11338.0\n",
      "2018-01-24 23:58:00  11427.00  11361.0\n",
      "2018-01-24 23:59:00  11427.88  11391.0\n",
      "\n",
      "[1440 rows x 2 columns]\n",
      "                        high      low\n",
      "candle_begin_time                    \n",
      "2018-01-24 00:01:00  10906.0  10871.0\n",
      "2018-01-24 00:02:00  10872.0  10840.0\n",
      "                        open      high      low     close  volume       北京时间\n",
      "candle_begin_time                                                           \n",
      "2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54  08点00分00秒\n",
      "2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05  08点01分00秒\n",
      "2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69  08点02分00秒\n",
      "2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13  08点03分00秒\n",
      "2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56  08点04分00秒\n",
      "...                      ...       ...      ...       ...     ...        ...\n",
      "2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43  07点55分00秒\n",
      "2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94  07点56分00秒\n",
      "2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94  07点57分00秒\n",
      "2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06  07点58分00秒\n",
      "2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19  07点59分00秒\n",
      "\n",
      "[1440 rows x 6 columns]\n",
      "10906.0\n"
     ]
    }
   ],
   "source": [
    "# iloc操作：通过position来读取数据\n",
    "print(df.iloc[0])  # 读第一行，iloc以index选取某一行，读取的数据是Series类型\n",
    "print(df.iloc[1:3])  # 选取在此范围内的多行，读取的数据是DataFrame类型\n",
    "print(df.iloc[:, 1:3])  # 选取在此范围内的多列，读取的数据是DataFrame类型\n",
    "print(df.iloc[1:3, 1:3])  # 读取指定的多行、多列，读取的数据是DataFrame类型\n",
    "print(df.iloc[:, :])  # 读取所有行、所有列，读取的数据是DataFrame类型\n",
    "print(df.iat[1, 1])  # 使用iat读取指定的某个元素。使用iloc也行，但是iat更高效。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
